package org.codingq92.network;

import java.net.InetAddress;

/**
 * @author: CodingQ92
 * @date: 2025/6/17 20:23
 */
public class InetAddr {
    public static void main(String[] args) throws Exception {
        // 获取本机器ip信息(hostname + ip)
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);
        // 获取主机名字 (hostname)
        String hostName = localHost.getHostName();
        System.out.println(hostName);
        // 获取ip地址
        String hostAddress = localHost.getHostAddress();
        System.out.println(hostAddress);

        System.out.println("--------------------------------------------------");

        // 获取指定ip或者域名对应的InetAddress
        InetAddress baidu = InetAddress.getByName("www.baidu.com");
        System.out.println(baidu);
        System.out.println(baidu.getHostName());
        System.out.println(baidu.getHostAddress());

        System.out.println("--------------------------------------------------");

        // 判断本机和指定的ip或者域名是否可以连接
        boolean reachable = baidu.isReachable(10000);
        System.out.println(reachable);
    }
}
